package com.ydd.controller.user;

import com.ydd.dto.UserLoginDTO;
import com.ydd.entity.YddUser;
import com.ydd.result.Result;
import com.ydd.service.IYddUserService;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.AccountNotFoundException;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/home/index")
@Slf4j
@Api(tags = "用户登录")
public class YddLoginController {

    @Autowired
    private IYddUserService yddUserService;

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) throws AccountNotFoundException {
        log.info("用户登录：{}", userLoginDTO);
        // 查询用户
        YddUser yddUser = yddUserService.login(userLoginDTO);
        //生成token
        String token = JwtUtil.getToken(yddUser.getId().toString());

        UserLoginVO employeeLoginVO = UserLoginVO.builder()
                .id(yddUser.getId().longValue())
                .name(yddUser.getUsername())
                .mobile(yddUser.getMobile())
                .token(token)
                .build();


        return Result.success(employeeLoginVO);
    }

}
